/**
 * 
 */
package ua.lviv.franko.visualisation;

import java.util.ArrayList;

import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Mapper;

import ua.lviv.franko.mlsFunctions.twoDimension.BaseFunction;
import ua.lviv.franko.mlsFunctions.twoDimension.Particle;
import ua.lviv.franko.mlsFunctions.twoDimension.Values;

/**
 * @author marko
 * 
 */
public class EFGResult2D extends Mapper {
	protected ArrayList<BaseFunction>	baseFunctions;
	protected ArrayList<Particle>		particles;
	protected double[]					res;

	public EFGResult2D(double[] res, ArrayList<BaseFunction> baseFunctions) {
		this.baseFunctions = baseFunctions;
		this.res = res;
	}

	public EFGResult2D(double[] res, ArrayList<BaseFunction> baseFunctions, ArrayList<Particle> particles) {
		this.baseFunctions = baseFunctions;
		this.res = res;
		this.particles = particles;
	}

	public void display(Range xrange, Range yrange) {
		Visualise3D.show(this, xrange, yrange);
	}

	@Override
	public double f(double x, double y) {
		if (particles != null) {
			double tmp = 0.0;
			for (int i = 0; i < baseFunctions.size(); i++) {
				BaseFunction bf = baseFunctions.get(i);
				Particle p = bf.getXi();
				if (p.lengthTo(new Particle(x, y, 0.0)) < p.getRho()) {
					Values[] v = bf.calculateAllFunctions(x, y);
					for (int j = 0; j < v.length; j++) {
						tmp += v[j].val * res[particles.indexOf(bf.getNodeList().get(j))];
					}
//					System.out.println(x + "\t" + y + "\t" + tmp);
					return tmp;
				}
			}

			return 0.0;
		} else {
			double tmp = 0.0;
			for (int i = 0; i < baseFunctions.size(); i++) {
				BaseFunction bFunction = baseFunctions.get(i);
				// tmp+=bFunction.calculateDerivatyY(x, y)*res[i];
				tmp += bFunction.calculate(x, y) * res[i];
			}
//			System.out.println(x + "\t" + y + "\t" + tmp);
			return tmp;// - (-5*(x*x) + 5*x);
		}
	}

}
